package com.algorithm.standard;

import com.google.common.collect.Range;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.shardingsphere.api.sharding.complex.ComplexKeysShardingAlgorithm;
import org.apache.shardingsphere.api.sharding.complex.ComplexKeysShardingValue;

import java.util.Collection;
import java.util.Map;

public class ComplexShardingAlgorithm implements ComplexKeysShardingAlgorithm<String> {

    final String create_time = "create_time";
    final String order_no = "order_no";

    public static void main(String[] args) {
        String str = "2020-01-04";
        System.out.println(str.substring(0, 10).replaceAll("-", "_"));
    }

    @Override
    public Collection<String> doSharding(Collection<String> availableTargetNames, ComplexKeysShardingValue<String> complexKeysShardingValue) {
        String tableName = complexKeysShardingValue.getLogicTableName();

        String shardingNum = null;
        Map<String, Collection<String>> map = complexKeysShardingValue.getColumnNameAndShardingValuesMap();
        for (Map.Entry<String, Collection<String>> entry : map.entrySet()) {
            if (CollectionUtils.isEmpty(entry.getValue())) {
                continue;
            }
            switch (entry.getKey()) {
                case create_time:

                    break;
                case order_no:
                    String orderNo = entry.getValue().iterator().next();
                    String yearmonth = orderNo.substring(9, 15);
//                    String quarter = DateUtil.getQuarter(yearmonth);

                    break;
            }

        }

        Map<String, Range<String>> map2 = complexKeysShardingValue.getColumnNameAndRangeValuesMap();

        for (Map.Entry<String, Range<String>> entry : map2.entrySet()) {
            switch (entry.getKey()) {
                case create_time:
                    String lower = entry.getValue().lowerEndpoint();
                    String upper = entry.getValue().upperEndpoint();

                    break;
                case order_no:
//                    String orderNo = entry.getValue();
//                    String yearmonth = orderNo.substring(9, 15);
//                    String quarter = DateUtil.getQuarter(yearmonth);

                    break;
            }

        }
        return null;
    }
}
